package utils

import (
	"encoding/json"
	"fmt"
	"reflect"
)

// Convert json string to map
func JsonToMap(json_byte []byte)(map[string]interface{}, error){
	m := make(map[string]interface{})
	err:=json.Unmarshal(json_byte, &m)
	if err != nil{
		fmt.Printf("Unmarshal with error: %+v\n", err)
		return nil, err
	}
	return m, nil
}

// Convert struct to map
func StructTomap(s interface{})map[string]interface{}{
	obj1 := reflect.TypeOf(s)
	obj2 := reflect.ValueOf(s)
	var data = make(map[string]interface{})
	for i := 0; i < obj1.NumField(); i++ {
		data[obj1.Field(i).Name] = obj2.Field(i).Interface()
	}
	return data
}

